from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column,String,TEXT,Integer,ForeignKey


#首先生成 orm基类
Base = declarative_base()

class ZhihuQuestion(Base):
    __tablename__ = "zhihu_question"
    """
    问题
    """
    id = Column(Integer, primary_key=True,autoincrement=True)
    question_id = Column(String(13),comment="问题的编号")
    title = Column(String(200),comment='问题的题目')
    content = Column(TEXT(2000),comment="问题的内容")
    comment_num = Column(Integer,comment='题目的评论数量',default=0)
    author_name = Column(String(20), comment='提问者名称')

class ZhihuAnswer(Base):
    __tablename__ = "zhihu_answer"
    """
    回答
    """
    id = Column(Integer, primary_key=True, autoincrement=True)
    content = Column(TEXT(2000), comment="问题的内容")
    comment_num = Column(Integer, comment='回答的评论数量', default=0)
    agree_num = Column(Integer, comment='回答的评论赞同数量', default=0)
    author_name = Column(String(20),comment='评论者名称')
    question_id = Column(Integer,ForeignKey("zhihu_question.id"),comment="回答的问题编号")

if __name__ == "__main__":
    engine = create_engine("mysql+pymysql://root:tusbasa1@127.0.0.1/scrapy_test2", encoding="utf-8")
    #Base.metadata.create_all(engine)
    from sqlalchemy.orm import sessionmaker
    Session = sessionmaker(bind=engine)
    zhihu_q = ZhihuQuestion(question_id='9123',title='asdf',content='sadf',comment_num=12,author_name='asdf')
    ss = Session()
    ss.add(zhihu_q)
    ss.flush()
    zhihu_a = ZhihuAnswer(content='asdf', comment_num=123, agree_num=123, author_name='adsf', question_id=zhihu_q.id)
    ss.add(zhihu_a)
    ss.commit()